﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections.ObjectModel;
using System.Text;

using System.Web.Routing;

namespace PhotoGallery.Infrastructure.Helpers
{
    public static class InputHelper
    {
        public static String DateInput(this HtmlHelper helper)
        {
            return DateInput(helper, new DateTime?());
        }

        private static string DateInput(HtmlHelper helper, DateTime? dateTime)
        {
            return DateInput(helper, dateTime, "yyyy-MM-dd", null);
        }

        public static String DateInput(this HtmlHelper helper, DateTime dateTime)
        {
            return DateInput(helper, dateTime, "yyyy-MM-dd", null);
        }

        public static String DateInput(this HtmlHelper helper, DateTime dateTime, object htmlAttributes)
        {
            return DateInput(helper, dateTime, "yyyy-MM-dd", htmlAttributes);
        }

        public static String DateInput(this HtmlHelper helper, DateTime? dateTime, object htmlAttributes)
        {
            return DateInput(helper, dateTime, "yyyy-MM-dd", htmlAttributes);
        }

        public static String DateInput(this HtmlHelper helper, DateTime dateTime, string format)
        {
            return DateInput(helper, dateTime, format, null);
        }

        public static String DateInput(this HtmlHelper helper, DateTime? dateTime, string format)
        {
            return DateInput(helper, dateTime, format, null);
        }

        public static String DateInput(this HtmlHelper helper, DateTime dateTime, string format, object htmlAttributes)
        {
            return DateInput(helper, new DateTime?(dateTime), format, htmlAttributes);
        }

        public static String DateInput(this HtmlHelper helper, DateTime? dateTime, string format, object htmlAttributes)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<input type=\"date\"");
            if (dateTime.HasValue)
            {
                sb.Append(" value=\"");
                sb.Append(dateTime.Value.ToString(format));
                sb.Append("\"");
            }

            if (htmlAttributes != null)
            {
                IDictionary<string, object> dics = new RouteValueDictionary(htmlAttributes);

                foreach (string key in dics.Keys)
                {
                    sb.Append(" ");
                    sb.Append(key);
                    sb.Append("=\"");
                    sb.Append(dics[key].ToString());
                    sb.Append("\"");
                }
            }
            sb.Append(" />");

            return sb.ToString();
        }
    }
}